<?php
defined('BASEPATH') or exit ('No direct script access allowed');

class User extends MY_Before_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->class_name           = strtolower(__CLASS__);
        $this->datas ['page_title'] = "人员管理";
    }

    public function index()
    {
        check_privilege(220);   //权限检测
        $this->datas ['trash_count'] = $this->myModel->get_trash_count();
        $this->query_list();
    }

    /**
     * 分页数据
     *
     * @param number $page
     */
    private function query_list($page = 1, $filter = array(), $load_page = TRUE)
    {
        $get = $this->input->get();
        if (!empty ($get)) {
            $filter = array_merge($filter, $get);
        }
        if (isset ($filter ['page']) && $filter ['page'] > 0) {
            $page = $filter ['page'];
        }

        $item_list             = $this->myModel->get_list($filter, $page);
        $this->datas ['lists'] = $item_list ['list'];
        $this->datas ['pager'] = $item_list ['pager'];
        if ($this->datas ['full_page']) {
            $this->load->view($this->dwt_list, $this->datas);
        } else {
            return $this->load->view($this->dwt_list, $this->datas, TRUE);
        }
    }

    /**
     * 接收ajax请求
     * @param unknown $action
     * @param number $item_id
     */
    public function ajax($action, $item_id = 0)
    {
        $datas            = $this->input->get();
        $datas['item_id'] = $item_id;
        $this->process_ajax($action, $datas);
    }

    /**
     * 接收表单post数据
     */
    public function post()
    {
        $post = $this->input->post();
        $this->process_ajax($post['action'], $post);
    }

    //跳转到详情页
    public function info($id)
    {
        $this->load->model('permission/role_model');
        $this->datas['role_list'] = $this->role_model->get_role_list();
        if ($id == 0) {
            check_privilege(221);   //权限检测
            $info['id']           = $id;
            $info['real_name']    = '';
            $info['gender']       = 0;
            $info['job']          = 0;
            $info['active']       = 1;
            $info['avatar']       = '';
            $info['avatar_thumb'] = '';
            $info['web_role_id']  = 0;
        } else {
            check_privilege(222);   //权限检测
            $info = $this->myModel->get_info($id);
        }
        $this->datas['form_action'] = $id == 0 ? 'insert' : 'update';
        $this->datas['page_title']  = $id > 0 ? '<span style="color: #428bca;">[' . $info["real_name"] . ']</span>' . "人员详情" : "添加人员";
        $this->datas['info']        = $info;
        $this->load->view($this->dwt_info, $this->datas);
    }

    /**
     * 处理各种ajax请求
     * @param unknown $action
     * @param unknown $datas
     * @return json
     */
    private function process_ajax($action, $datas = array())
    {
        $content        = '';
        $message        = '';
        $append_data    = array();
        $exclude_for_id = array('add', 'insert', 'update', 'query');
        if ($action == 'update') {
            $datas['item_id'] = $datas['id'];
        }
        if (!in_array($action, $exclude_for_id) && (!isset($datas['item_id']) || empty($datas['item_id']))) {
            make_json_error("未指明操作对象！[305:$action]");
        }

        $result['error'] = 0;
        switch ($action) {
            case 'query':
                check_privilege(220);   //权限检测
                $this->datas ['full_page'] = FALSE;
                $content                   = $this->query_list();
                //统计数量
                $append_data['trash_count'] = $this->myModel->get_trash_count();
                break;
            case 'insert':
                check_privilege(221);   //权限检测
                $result = $this->myModel->add_data($datas);
                if ($result['error'] == 1) {
                    make_json_error($result['message']);
                } else {
                    $append_data['followUp'] = 'redirect';
                    $append_data['redirect'] = site_url($this->datas['my_router']);
                };
                break;
            case 'update':
                check_privilege(222);   //权限检测
                $result = $this->myModel->edit_data($datas, $datas['item_id'], 'update', 'fast_edit');
                if ($result['error'] == 1) {
                    make_json_error($result['message']);
                } else {
                    $append_data['followUp'] = 'redirect';
                    $append_data['redirect'] = site_url($this->datas['my_router']);
                }
                break;
            case 'respwd':
                check_privilege(224);   //权限检测
                $datas['password']       = md5(strtolower(123456));
                $result                  = $this->myModel->edit_data($datas, $datas['item_id']);
                $append_data['followUp'] = 'queryList';
                break;
            case 'hide':
            case 'show':
                check_privilege(222);   //权限检测
                $update_datas['active'] = $action == 'hide' ? 0 : 1;
                $result                 = $this->myModel->edit_data($update_datas, $datas['item_id']);
                break;
            case 'restore':
            case 'delete':
                check_privilege(223);   //权限检测

                $update_datas['is_delete'] = $action == 'delete' ? 1 : 0;
                $result                    = $this->myModel->edit_data($update_datas, $datas['item_id']);
                $append_data['followUp']   = 'queryList';
                break;
            default:
                $result['error']   = 300;
                $result['message'] = "非法操作！[" . $datas['item_id'] . "]";
                break;
        }
        if ($result['error'] > 0) {
            make_json_error($result['message']);
        } else {
            make_json_result($content, $message, $append_data);
        }
    }

    /**
     * 接收表格批量数据
     */
    public function batch()
    {
        $post            = $this->input->post();
        $content         = '';
        $message         = '';
        $append_data     = array();
        $result['error'] = 0;
        $action          = $post['action'];
        $items_id        = !empty($post['checkboxes']) ? $post['checkboxes'] : '';

        // 这里是过滤掉超级管理员
        $user_list = $this->myModel->get_filter_list($items_id, 'is_web_admin_role,id');
        foreach ($user_list as $k => $v) {
            if (($v['is_web_admin_role'] == 1 && $_SESSION['user_id'] != $v['id']) || ($v['is_web_admin_role'] == 1 && in_array($action, ['delete', 'restore']))) {
                unset($items_id[$k]);
            }
        }

        switch ($action) {
            case 'show':
            case 'hide':
                check_privilege(223);   //权限检测
                $update_datas['active'] = $action == 'hide' ? 0 : 1;
                //批量更新
                $result = $this->myModel->edit_data($update_datas, $items_id, TRUE);
                break;
            case 'restore':
            case 'delete':
                check_privilege(223);   //权限检测
                $update_datas['is_delete'] = $action == 'delete' ? 1 : 0;
                $result                    = $this->myModel->edit_data($update_datas, $items_id, TRUE);
                break;
            default:
                $result['error']   = 300;
                $result['message'] = "非法操作！[" . $action . "]";
                break;
        }
        $append_data['followUp'] = 'queryList';
        if ($result['error'] > 0) {
            make_json_error($result['message'] . "\r\n[Err:" . $result['error'] . "]");
        } else {
            make_json_result($content, $message, $append_data);
        }
    }


}
